package com.hdax.controller;

import com.hdax.entity.Department;
import com.hdax.entity.EmployeeInfo;
import com.hdax.entity.Role;
import com.hdax.service.DepartmentService;
import com.hdax.service.RoleService;
import com.hdax.utils.ResultUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色控制器
 *
 * @author Aizen
 * @date 2022/05/10
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 角色服务
     */
    @Autowired
    private RoleService roleService;


    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


    /**
     * 查询角色
     *
     * @return {@link String}
     */
    @GetMapping("/index")
    public String index(){
        return "system/role";
    }

    /**
     * 查询角色
     * @return {@link String}
     */
    @ResponseBody
    @GetMapping("/list")
    public Map<String,Object> findRole(@RequestParam("page")int page, @RequestParam("limit")int limit, @Param("employee") Role role){
        List<Role> allRoles = roleService.findAllRoles((page-1)*limit, limit, role);
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data",allRoles);
        map.put("count",roleService.findCount());
        return map;
    }


    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    /**
     * 添加角色
     *
     * @return {@link String}
     */
    @ResponseBody
    @GetMapping("/add")
    public ModelAndView addRole(Model model){
        ModelAndView modelAndView = new ModelAndView("system/addRole");
        //查询所有部门
        List<Department> departmentList = departmentService.findAll();
        model.addAttribute("departmentList",departmentList);
        return modelAndView;
    }

    /**
     * 添加角色
     *
     * @param role 角色
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    @ResponseBody
    @PostMapping("/add")
    public ResultUtil addRole(Role role){
        try {
            roleService.addRole(role);
        } catch (Exception e) {
            return ResultUtil.upDataResult("添加失败");
        }
        return ResultUtil.upDataResult("添加成功！");
    }

    //--------------------------------------删除角色-------------------------------------------------------------

    @ResponseBody
    @PostMapping("/delete")
    public ResultUtil deleteRole(Integer roleId){
        //查询角色占用数
        int count = roleService.findCountByRole(roleId);
        //该角色没人占用则可以删除
        if(count==0){
            try {
                roleService.deleteRole(roleId);
                return ResultUtil.upDataResult("删除成功！");
            } catch (Exception e) {
                return ResultUtil.upDataResult("删除失败");
            }
        }
        return ResultUtil.upDataResult("删除失败");
    }

    //--------------------------------------修改角色-------------------------------------------------------------

    /**
     * 编辑角色
     *
     * @return {@link String}
     */
    @ResponseBody
    @GetMapping("/editRole")
    public ModelAndView editRole(Integer roleId){
        //查询所有部门
        List<Department> departmentList = departmentService.findAll();
        Role role = roleService.findById(roleId);
        ModelAndView modelAndView = new ModelAndView("system/editRole");
        modelAndView.addObject("role",role);
        modelAndView.addObject("departmentList",departmentList);
        return modelAndView;
    }

    @ResponseBody
    @PostMapping("/editRole")
    public ResultUtil editRole(Role role){
        try {
            roleService.updateRole(role);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.upDataResult("修改失败");
        }
        return ResultUtil.upDataResult("修改成功！");
    }

}
